//package xmlTranslators;
package cpre556.project2.eclipsePlugin.support;

import java.io.*;
import java.util.Vector;

public class SPLINTtoXML extends Plugin{	
	private FileInputStream fis = null;
	private BufferedReader reader = null;
	
	public SPLINTtoXML(){
		super();
	}
	
	protected void parseFile(){		
		try{
			fis = new FileInputStream(saver.getTxt());
			reader = new BufferedReader(new InputStreamReader(fis));
			
			vulnerabilityList = new Vector<Vulnerability>();
			Vulnerability tempVulnerability;					
			
			String inLine;
			
			while((inLine = reader.readLine()) != null){                                
				if(inLine.indexOf("(in function ") == -1){
					tempVulnerability = new Vulnerability();
				
					String fn = inLine.substring(0, inLine.indexOf("("));
				
					tempVulnerability.setFilename(fn);
					String ln = inLine.substring(inLine.indexOf("("), inLine.indexOf(")") + 1);
				
					if(ln.indexOf(",")!=-1){
						ln = ln.substring(ln.indexOf("(")+1, ln.indexOf(","));
						tempVulnerability.setLineNumber(Integer.valueOf(ln).intValue());
					}
					
					tempVulnerability.setFunctionName(" ");
				
				
					tempVulnerability.setPriority("None given.");
				
					inLine = reader.readLine();
								
					String desc = inLine;
					inLine = reader.readLine();
					if((inLine.length() > 3)){
						while((inLine.indexOf(".c")) == -1){
							desc = desc + inLine;
							inLine = reader.readLine();
						}
						
						if(desc.indexOf("(Use") > 0){
							desc = desc.substring(0, desc.indexOf("(Use"));
						}
						
						tempVulnerability.setDescription(desc);
						tempVulnerability.setSuggestion("None given.");
						vulnerabilityList.add(tempVulnerability);
					}
					else{
						inLine = reader.readLine();	
						inLine = reader.readLine();
					}
				}
				else
					inLine = reader.readLine();
			}
			
			reader.close();
		}
		catch(FileNotFoundException fnfe){
			System.err.println("File not found:" + saver.getTxt() +".");			
		}
		catch(IOException ioe){
			System.err.println("I/O error:" + ioe);
		}
	}
}